<?php
/**
 * @Notes:【】
 * @Date: 2022-06-05 15:23
 */

namespace Form;

use App\Dao\Product\StoreCategoryDao;
use App\Dao\Task\TaskHelpDao;
use App\Model\StoreCategory;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/productCategory")
 */
class ProductCategory extends BaseForm
{
    protected $yjCode = 'question';

    protected $routes = [
        'lists' => '/admin/product/category/lists',
        'add' => '/admin/product/category/add',
        'edit' => '/admin/product/category/edit',
        'delete' => '/admin/product/category/delete',
        'form' => '/form/productCategory/update',
    ];

    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('ID')->setProp('id'),
            Column::txt()->setLabel('标题')->setProp('cate_name'),
            Column::image()->setLabel('图标')->setProp('pic'),
            Column::txt()->setLabel('排序')->setProp('sort'),
            Column::txt()->setLabel('状态')->setProp('is_show'),
        ]);
        return $colum->toArray();
    }

    protected function getRoute($types = null)
    {
        if (is_null($types)) {
            return $this->routes;
        }
        return $this->routes[$types];
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $routes = $this->getRoute();
        $column = $this->getColumn();
        _SUCCESS(compact('routes', 'column'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id', 0);
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));
        $form->setRule([
            Elm::select('pid', '上级分类')->options(function ($item) {
                $storeProductDao = App(StoreCategoryDao::class)
                    ->select(['id as value', 'cate_name as label'])
                    ->where('pid', 0)->get();
                $storeProductDao->prepend(['value' => 0, 'label' => '顶级菜单']);
                return $storeProductDao->toArray();
            })->required(),
            Elm::input('cate_name', '名称')->required(),
            Elm::YjUpload()->field('pic')->title('图标'),
            Elm::number('sort', '排序')->required(),
            Elm::radio('is_show', '状态')->options(function () {
                return [
                    [
                        'label' => '显示',
                        'value' => 1,
                    ],
                    [
                        'label' => '隐藏',
                        'value' => 0,
                    ],
                ];
            })
        ]);

        $id && $form->append(
            Elm::hidden('id', $id)
        );

        $lists = $form->setTitle($id ? '编辑' : '添加' . '商品分类')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(StoreCategoryDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }
}